<template>
  <view class="activity-page">
    <!-- 搜索栏 -->
    <view class="search-section">
      <u-search 
        v-model="searchKeyword" 
        placeholder="搜索活动名称"
        :show-action="false"
        bg-color="#f5f5f5"
        @search="handleSearch"
        @clear="handleClear"
      ></u-search>
    </view>

    <!-- 分类筛选 -->
    <view class="filter-section">
      <scroll-view scroll-x class="filter-scroll">
        <view class="filter-list">
          <view 
            v-for="(item, index) in filterList" 
            :key="index"
            class="filter-item"
            :class="{ active: currentFilter === item.value }"
            @click="handleFilterChange(item.value)"
          >
            <text class="filter-text">{{ item.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 活动列表 -->
    <view class="activity-list">
      <view 
        v-for="(item, index) in activityList" 
        :key="item.id"
        class="activity-item"
        @click="handleItemClick(item)"
      >
        <!-- 活动状态标签 -->
        <view class="status-badge" :class="getStatusClass(item.status)">
          <text class="status-text">{{ item.statusText }}</text>
        </view>

        <!-- 活动图片 -->
        <view class="activity-image">
          <image :src="item.image" mode="aspectFill" class="image"></image>
        </view>

        <!-- 活动内容 -->
        <view class="activity-content">
          <view class="content-header">
            <text class="activity-title">{{ item.title }}</text>
            <view class="activity-type">
              <u-tag 
                :text="item.type" 
                type="primary"
                size="mini"
              ></u-tag>
            </view>
          </view>
          
          <view class="content-body">
            <text class="activity-description">{{ item.description }}</text>
          </view>
          
          <view class="content-footer">
            <view class="activity-info">
              <view class="info-item">
                <u-icon name="calendar" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.startTime }}</text>
              </view>
              <view class="info-item">
                <u-icon name="map-pin" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.location }}</text>
              </view>
              <view class="info-item">
                <u-icon name="users" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.registeredCount }}/{{ item.maxParticipants }}人</text>
              </view>
            </view>
            
            <view class="activity-price">
              <text class="price-text" :class="{ free: item.price === 0 }">
                {{ item.price === 0 ? '免费' : `¥${item.price}` }}
              </text>
            </view>
          </view>
        </view>

        <!-- 报名按钮 -->
        <view class="registration-btn" :class="getButtonClass(item.status)" @click.stop="handleRegistration(item)">
          <text class="btn-text">{{ getButtonText(item.status) }}</text>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="activityList.length === 0" class="empty-state">
      <u-icon name="calendar" size="48" color="#ccc"></u-icon>
      <text class="empty-text">暂无活动信息</text>
    </view>

    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more">
      <u-loading-icon v-if="loading" mode="circle"></u-loading-icon>
      <text v-else class="load-text" @click="loadMore">点击加载更多</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

// 搜索关键词
const searchKeyword = ref('')

// 当前筛选条件
const currentFilter = ref('all')

// 筛选列表
const filterList = reactive([
  { label: '全部', value: 'all' },
  { label: '进行中', value: 'ongoing' },
  { label: '即将开始', value: 'upcoming' },
  { label: '已结束', value: 'ended' },
  { label: '免费活动', value: 'free' },
  { label: '付费活动', value: 'paid' }
])

// 活动列表数据
const activityList = ref([
  {
    id: 1,
    title: '社区春节联欢晚会',
    description: '社区将于2月8日晚举办春节联欢晚会，现开始报名，欢迎有才艺的居民积极参与...',
    type: '文艺演出',
    status: 1,
    statusText: '报名中',
    startTime: '2025-02-10 19:00',
    location: '社区活动中心',
    registeredCount: 45,
    maxParticipants: 100,
    price: 0,
    image: 'https://example.com/images/spring-festival.jpg'
  },
  {
    id: 2,
    title: '健康义诊活动',
    description: '社区联合市人民医院开展健康义诊活动，为居民提供免费体检和健康咨询...',
    type: '健康服务',
    status: 2,
    statusText: '即将开始',
    startTime: '2025-01-25 09:00',
    location: '社区广场',
    registeredCount: 78,
    maxParticipants: 80,
    price: 0,
    image: 'https://example.com/images/health-check.jpg'
  },
  {
    id: 3,
    title: '亲子运动会',
    description: '为增进亲子关系，社区举办亲子运动会，设置多个趣味项目，欢迎家长和孩子一起参与...',
    type: '体育竞技',
    status: 1,
    statusText: '报名中',
    startTime: '2025-02-15 14:00',
    location: '社区体育场',
    registeredCount: 32,
    maxParticipants: 50,
    price: 20,
    image: 'https://example.com/images/family-sports.jpg'
  },
  {
    id: 4,
    title: '书法培训班',
    description: '邀请知名书法家为社区居民开设书法培训班，传承中华文化，提升艺术修养...',
    type: '教育培训',
    status: 1,
    statusText: '报名中',
    startTime: '2025-02-01 09:00',
    location: '社区文化室',
    registeredCount: 15,
    maxParticipants: 25,
    price: 100,
    image: 'https://example.com/images/calligraphy.jpg'
  },
  {
    id: 5,
    title: '环保知识讲座',
    description: '邀请环保专家为居民讲解环保知识，提高环保意识，共建绿色社区...',
    type: '知识讲座',
    status: 3,
    statusText: '已结束',
    startTime: '2025-01-20 15:00',
    location: '社区会议室',
    registeredCount: 60,
    maxParticipants: 60,
    price: 0,
    image: 'https://example.com/images/environment.jpg'
  }
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 获取状态样式类
const getStatusClass = (status: number) => {
  switch (status) {
    case 1: return 'status-ongoing'
    case 2: return 'status-upcoming'
    case 3: return 'status-ended'
    default: return 'status-default'
  }
}

// 获取按钮样式类
const getButtonClass = (status: number) => {
  switch (status) {
    case 1: return 'btn-register'
    case 2: return 'btn-upcoming'
    case 3: return 'btn-ended'
    default: return 'btn-default'
  }
}

// 获取按钮文本
const getButtonText = (status: number) => {
  switch (status) {
    case 1: return '立即报名'
    case 2: return '即将开始'
    case 3: return '已结束'
    default: return '查看详情'
  }
}

// 搜索处理
const handleSearch = (value: string) => {
  console.log('搜索活动:', value)
  // 这里实现搜索逻辑
}

// 清空搜索
const handleClear = () => {
  searchKeyword.value = ''
  console.log('清空搜索')
}

// 筛选条件改变
const handleFilterChange = (value: string) => {
  currentFilter.value = value
  console.log('筛选条件改变:', value)
  // 这里实现筛选逻辑
}

// 点击活动项
const handleItemClick = (item: any) => {
  console.log('点击活动:', item)
  // 跳转到活动详情页面
  uni.navigateTo({
    url: `/pages/activity-registration/detail?id=${item.id}`
  })
}

// 报名处理
const handleRegistration = (item: any) => {
  if (item.status === 1) {
    // 跳转到报名页面
    uni.navigateTo({
      url: `/pages/activity-registration/register?id=${item.id}`
    })
  } else if (item.status === 2) {
    uni.showToast({
      title: '活动尚未开始报名',
      icon: 'none'
    })
  } else if (item.status === 3) {
    uni.showToast({
      title: '活动已结束',
      icon: 'none'
    })
  }
}

// 加载更多
const loadMore = () => {
  if (loading.value) return
  
  loading.value = true
  setTimeout(() => {
    // 模拟加载更多数据
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  console.log('活动报名页面加载完成')
})
</script>

<style lang="scss" scoped>
.activity-page {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 0 0 20rpx 0;
}

.page-header {
  background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
  padding: 20rpx 30rpx 30rpx;
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .header-title {
      .title-text {
        display: block;
        font-size: 36rpx;
        font-weight: 600;
        color: #fff;
        margin-bottom: 8rpx;
      }
      
      .title-subtitle {
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.8);
      }
    }
    
    .header-icon {
      width: 80rpx;
      height: 80rpx;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.search-section {
  padding: 24rpx 32rpx;
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.filter-section {
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  
  .filter-scroll {
    white-space: nowrap;
    padding: 0 32rpx 24rpx 32rpx;
    
    .filter-list {
      display: flex;
      padding: 24rpx 0 0 0;
      
      .filter-item {
        flex-shrink: 0;
        padding: 16rpx 28rpx;
        margin-right: 20rpx;
        border-radius: 24rpx;
        background-color: #f8f9fa;
        transition: all 0.3s ease;
        border: 2rpx solid transparent;
        
        &.active {
          background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
          transform: translateY(-2rpx);
          box-shadow: 0 4rpx 12rpx rgba(82, 196, 26, 0.25);
          
          .filter-text {
            color: #fff;
          }
        }
        
        .filter-text {
          font-size: 28rpx;
          color: #666;
          font-weight: 500;
          white-space: nowrap;
        }
      }
    }
  }
}

.activity-list {
  padding: 0 32rpx;
  
  .activity-item {
    background-color: #fff;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    padding: 32rpx;
    position: relative;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    border: 1rpx solid rgba(0, 0, 0, 0.04);
    
    &:active {
      transform: translateY(-2rpx);
      box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
    }
    
    .status-badge {
      position: absolute;
      top: 20rpx;
      right: 20rpx;
      padding: 8rpx 16rpx;
      border-radius: 20rpx;
      font-size: 20rpx;
      
      &.status-ongoing {
        background-color: #52c41a;
        color: #fff;
      }
      
      &.status-upcoming {
        background-color: #faad14;
        color: #fff;
      }
      
      &.status-ended {
        background-color: #d9d9d9;
        color: #666;
      }
    }
    
    .activity-image {
      width: 100%;
      height: 300rpx;
      border-radius: 12rpx;
      overflow: hidden;
      margin-bottom: 20rpx;
      
      .image {
        width: 100%;
        height: 100%;
      }
    }
    
    .activity-content {
      .content-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 16rpx;
        
        .activity-title {
          flex: 1;
          font-size: 32rpx;
          font-weight: 600;
          color: #333;
          line-height: 1.4;
          margin-right: 20rpx;
        }
        
        .activity-type {
          flex-shrink: 0;
        }
      }
      
      .content-body {
        margin-bottom: 20rpx;
        
        .activity-description {
          font-size: 28rpx;
          color: #666;
          line-height: 1.5;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
      
      .content-footer {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        margin-bottom: 80rpx;
        
        .activity-info {
          display: flex;
          flex-direction: column;
          gap: 12rpx;
          flex: 1;
          
          .info-item {
            display: flex;
            align-items: center;
            gap: 8rpx;
            
            .info-text {
              font-size: 24rpx;
              color: #999;
            }
          }
        }
        
        .activity-price {
          margin-right: 20rpx;
          
          .price-text {
            font-size: 28rpx;
            font-weight: 600;
            color: #ff4d4f;
            
            &.free {
              color: #52c41a;
            }
          }
        }
      }
    }
    
    .registration-btn {
      position: absolute;
      bottom: 30rpx;
      right: 30rpx;
      padding: 16rpx 32rpx;
      border-radius: 40rpx;
      font-size: 24rpx;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &.btn-register {
        background-color: #52c41a;
        color: #fff;
        
        &:active {
          background-color: #389e0d;
        }
      }
      
      &.btn-upcoming {
        background-color: #faad14;
        color: #fff;
      }
      
      &.btn-ended {
        background-color: #d9d9d9;
        color: #666;
      }
      
      .btn-text {
        font-size: 24rpx;
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-text {
    margin-top: 20rpx;
    font-size: 28rpx;
    color: #999;
  }
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx 0;
  
  .load-text {
    font-size: 28rpx;
    color: #52c41a;
  }
}
</style>
